package com.stps;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.stps.dao.BaseDao;

public class JDBCDemo {
	static  String driverName = null;// "com.mysql.jdbc.Driver";
	static  String url  = null;// "jdbc:mysql://localhost:3306/daodemo?useUnicode=true&characterEncoding=utf-8";
	static  String user  = null;//  "root";
	static  String password  = null;// "123456";
	static{//静态代码块,在类加载的时候执行
		init();
	}	
	
    //初始化连接参数,从配置文件里获得
	public static void init(){
		Properties params=new Properties();
		String configFile = "db-daodemo.properties";
		InputStream is=BaseDao.class.getClassLoader().getResourceAsStream(configFile);
		try {
			params.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		driverName = params.getProperty("driver");
		url=params.getProperty("url");
		user=params.getProperty("user");
		password=params.getProperty("password");
	} 

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 向程序中注册加载的驱动程序
        Class.forName(driverName);
        //得到连接对象
        Connection conn= DriverManager.getConnection(url,user,password);
        //System.out.println(connection);
        
        /*
        Statement smt = conn.createStatement();
        String sqlStr = "select * from student;";
        ResultSet rs = smt.executeQuery(sqlStr);
        while(rs.next()) {
        	System.out.println(rs.getString("sno")+"   "+rs.getString("name")+"   "+rs.getString("sex"));
        }
        */
        String sqlStr = "select * from student where sno=?;";
        PreparedStatement pstmt = conn.prepareStatement(sqlStr);
        pstmt.setString(1, "1");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
        	System.out.println(rs.getString("sno")+"   "+rs.getString("name")+"   "+rs.getString("sex"));
        }        
        
        rs.close();
        pstmt.close();
        conn.close();
    }
}
